//
//  SISSExcelLeftTableView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/8/6.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSExcelLeftTableView.h"
#import "SISSExcelLeftTableViewCell.h"
#import "SISSExcelView.h"

NSString* const leftIdentifier_Excel = @"LeftContentTableViewCell";
@interface SISSExcelLeftTableView ()<UITableViewDelegate,UITableViewDataSource>

@end
@implementation SISSExcelLeftTableView

- (void)dealloc {
    self.delegate = nil;
    self.dataSource = nil;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[SISSExcelLeftTableViewCell class] forCellReuseIdentifier:leftIdentifier_Excel];
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    //通知contentTableView滑动
    [self setValue:[NSValue valueWithCGPoint:scrollView.contentOffset] forKey:Excel_leftTableViewContentOffset];
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.excelView.dataSource respondsToSelector:@selector(contentTableView:numberOfRowsInSection:)]) {
        return [self.excelView.dataSource contentTableView:nil numberOfRowsInSection:section];
    }
    return 100;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    SISSExcelLeftTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:leftIdentifier_Excel forIndexPath:indexPath];

    if ([self.excelView.dataSource respondsToSelector:@selector(leftTableView:cellForRowAtIndexPath:)]) {
        cell.customLabel.text = [self.excelView.dataSource leftTableView:self cellForRowAtIndexPath:indexPath];
    } else {
        cell.customLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    CGFloat height = self.excelView.contentHeight;
    if ([self.excelView.dataSource respondsToSelector:@selector(heightOfContentTableView:cellForRowAtIndexPath:)]) {
        height = [self.excelView.dataSource heightOfContentTableView:nil cellForRowAtIndexPath:indexPath];
    }
    return height;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    //使用 self.seletedPath 会触发kvo
    _seletedPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    [self setValue:_seletedPath forKey:Excel_seletedPath];
    
    if ([self.excelView.delegate respondsToSelector:@selector(leftTableView:didSelectRowAtIndexPath:)]) {
        [self.excelView.delegate leftTableView:self didSelectRowAtIndexPath:indexPath];
    }
}


@end
